package com.gzcsqic.excelapp.common;


import java.util.HashMap;

public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";

    /**
     * 返回内容
     */
    public static final String MSG_TAG = "msg";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    /**
     * 状态类型
     */
    public enum Type {
        /**
         * 成功
         */
        SUCCESS(0),
        /**
         * 警告
         */
        WARN(301),
        /**
         * 错误
         */
        ERROR(500);
        private final int value;

        Type(int value) {
            this.value = value;
        }

        public int value() {
            return this.value;
        }
    }

    public R() {
    }

    public R(Type type, String msg) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
    }

    public R(Type type, String msg, Object data) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }

    public R(String code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }

    public static R success() {
        return R.success("操作成功");
    }

    public static R success(Object data) {
        return R.success(null, data);
    }

    public static R success(String msg) {
        return R.success(msg, null);
    }

    public static R success(String msg, Object data) {
        return new R(Type.SUCCESS, msg, data);
    }

    public static R warn(String msg) {
        return R.warn(msg, null);
    }

    public static R warn(String msg, Object data) {
        return new R(Type.WARN, msg, data);
    }

    public static R error() {
        return R.error("操作失败");
    }

    public static R error(String msg) {
        return new R(Type.ERROR, msg, null);
    }

    public static R error(String msg, Object data) {
        return new R(Type.ERROR, msg, data);
    }

    public static R error(String code,String msg) {
        return R.error(code, msg, null);
    }

    public static R error(String code,String msg, Object data) {
        return new R(code, msg, data);
    }

}
